package com.aricwang.easyprofile.place;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

import com.aricwang.easyprofile.contentprovider.PlaceContentProvider;
import com.aricwang.easyprofile.database.PlaceTable;
import com.aricwang.easyprofile.rule.Rule;

public class Place {
	public static final int RADIUS_MAX = 2000;	// meters
	public static final int RADIUS_DEFAULT = 800; // meters

	public int mPlaceId;
	public String mName = "";
	public String mAddress = "";
	public double mLatitude = 0;
	public double mLongitude = 0;
	public int mRadius = RADIUS_DEFAULT;
	
	/////////////////////////////////////////////////////////////////////////////////////////
	// Static functions
	/////////////////////////////////////////////////////////////////////////////////////////

	// get Place from database
	public static Place getPlace(Context context, int id) {
		Place o = null;
		
		Uri uri = Uri.parse(PlaceContentProvider.CONTENT_URI + "/" + id);

		Cursor c = null;
		try {
			c = context.getContentResolver().query(uri, PlaceTable.ALL_COLUMNS, null, null, null); 
		} catch (Exception e){
			throw  new IllegalArgumentException("ERROR");
		};
		
		if (c == null)
			return o;
		
		if (c.getCount() > 0) {
			// must move to first, otherwise, get exception
			c.moveToFirst();
			
			o = new Place();
			
			// get the item from database
			o.mPlaceId = c.getInt(c.getColumnIndex(PlaceTable.COLUMN_ID));
			o.mName = c.getString(c.getColumnIndex(PlaceTable.COLUMN_NAME));
			o.mAddress = c.getString(c.getColumnIndex(PlaceTable.COLUMN_ADDRESS));
			o.mLatitude = c.getDouble(c.getColumnIndex(PlaceTable.COLUMN_LATITUDE));
			o.mLongitude = c.getDouble(c.getColumnIndex(PlaceTable.COLUMN_LONGITUDE));
			o.mRadius = c.getInt(c.getColumnIndex(PlaceTable.COLUMN_RADIUS));
		}
	
		c.close();
		
		return o;
	}

	
	public static int deletePlace(Context context, int id) {
		Uri uri = Uri.parse(PlaceContentProvider.CONTENT_URI + "/" + id);
		return context.getContentResolver().delete(uri, null, null);
	}
	
	public static Uri insertPlace(Context context, Place o) {
		ContentValues values = placeToContentValues(o);
		return context.getContentResolver().insert(PlaceContentProvider.CONTENT_URI, values);
	}
	
	public static int updatePlace(Context context, Place o) {
		Uri uri = Uri.parse(PlaceContentProvider.CONTENT_URI + "/" + o.mPlaceId);
		ContentValues values = placeToContentValues(o);
		return context.getContentResolver().update(uri, values, null, null);
	}
	
	private static ContentValues placeToContentValues(Place o) {
		ContentValues values = new ContentValues();
		values.put(PlaceTable.COLUMN_NAME, o.mName);
		values.put(PlaceTable.COLUMN_ADDRESS, o.mAddress);
		values.put(PlaceTable.COLUMN_LATITUDE, o.mLatitude);
		values.put(PlaceTable.COLUMN_LONGITUDE, o.mLongitude);
		values.put(PlaceTable.COLUMN_RADIUS, o.mRadius);

		return values;
	}
	
	static public String getPlaceNameById(Context context, int id) {
		String name = "";
		
    	Cursor cursor = context.getContentResolver().query(PlaceContentProvider.CONTENT_URI, 
				new String[] { PlaceTable.COLUMN_ID, PlaceTable.COLUMN_NAME}, 
				PlaceTable.COLUMN_ID + " = " + id, null, null);
		if (cursor != null) {
			if (cursor.getCount() > 0) {
				cursor.moveToFirst();
				name = cursor.getString(cursor.getColumnIndex(PlaceTable.COLUMN_NAME));
			}
			cursor.close();
		}

		return name;
	}
	
	static public int getPlaceIdByName(Context context, String name) {
		int id = Rule.NO_PLACE;
		
    	Cursor cursor = context.getContentResolver().query(PlaceContentProvider.CONTENT_URI, 
				new String[] { PlaceTable.COLUMN_ID, PlaceTable.COLUMN_NAME}, 
				PlaceTable.COLUMN_NAME + " = '" + name + "'", null, null);
		if (cursor != null) {
			if (cursor.getCount() > 0) {
				cursor.moveToFirst();
				id = cursor.getInt(cursor.getColumnIndex(PlaceTable.COLUMN_ID));
			}
			cursor.close();
		}

		return id;
	}
	
}
